
package com.sz.sidi.oa.splashHandlers;


import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.splash.AbstractSplashHandler;

import com.sz.sidi.oa.ui.login.LoginComposite;

/**
 * @since 3.3
 * 
 */
public class InteractiveSplashHandler extends AbstractSplashHandler {
	
	private Shell splash;
	private LoginComposite container;
	
	private void replaceShell(Shell splash) {
		Shell newSplash = new Shell(Display.getCurrent(), SWT.NO_TRIM);
		newSplash.setBackgroundImage(splash.getBackgroundImage());
		newSplash.setBounds(splash.getBounds());
		newSplash.setFont(splash.getFont());
		newSplash.setVisible(true);

		Image small = new Image(splash.getDisplay(), 16, 16);
		GC gc = new GC(small);
		gc.setBackground(splash.getDisplay().getSystemColor(SWT.COLOR_RED));
		gc.fillArc(0, 0, 16, 16, 45, 270);
		gc.dispose();

		newSplash.setImage(small);
		newSplash.setText("Sidi办公自动化系统");

		setSplash(newSplash);
	}

	public Shell getSplash() {
		return splash;
	}

	public void setSplash(Shell splash) {
		this.splash = splash;
	}

	/**
	 * 
	 */
	public InteractiveSplashHandler() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets
	 * .Shell)
	 */
	public void init(final Shell splash) {
		replaceShell(splash);//
		super.init(getSplash());
		getSplash().forceActive();
		createUI();
		splash.dispose();
		getSplash().layout(true);
		doEventLoop();
	}

	/**
	 * 
	 */
	private void doEventLoop() {
		Shell splash = getSplash();
		while (container.isFinished() == false) {
			if (splash.getDisplay().readAndDispatch() == false) {
				splash.getDisplay().sleep();
			}
		}
	}

	/**
	 * 
	 */
	private void createUI() {
		splash.setLayout(new FillLayout());
		container = new LoginComposite(splash,SWT.NONE);
		container.layout();
	}

	
}
